﻿using AutoMapper;
using Common.Communal;
using ERP.Domain.System;
using MediatR;
using System.Api.Write.Applications.Commands.Users;
using System.Services.Users;

namespace System.Api.Write.Applications.CommandHandlers.Users
{
    /// <summary>
    /// 用户创建命令处理程序
    /// </summary>
    public class UserCreateCommandHandler : IRequestHandler<UserCreateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 用户服务接口
        /// </summary>
        private readonly IUserService userService;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数，注入用户服务接口
        /// </summary>
        /// <param name="userService">用户服务接口</param>
        public UserCreateCommandHandler(IUserService userService,IMapper mapper)
        {
            this.userService = userService;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理用户创建命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(UserCreateCommand request, CancellationToken cancellationToken)
        {
            var res = mapper.Map<User>(request);
            return await userService.Add(res);
        }
    }
}
